import router from '@/router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import store from '@/store'

/**
 *前置守卫
 *
 */

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  if (store.getters.token) {
    // 存在token
    if (to.path === '/login') {
      // 跳转到主页
      next('/') // 中转到主页
      // next（地址）并没有执行后置守卫
      NProgress.done()
    } else {
      // 判断是否获取过资料
      if(!store.getters.userId){
        await store.dispatch('user/getUserInfo')
      }
      next()// 放行

    }
  } else {
    // 没有token
    if(whiteList.includes(to.path)){
      next()
    }else{
      next('/login')
      NProgress.done()
    }

  }
})

/** *
 * 后置守卫
 * **/
router.afterEach(() => {
  NProgress.done()
})
